CRUD Example : Company Master

  • 1. View Pages & Navigation

    STEP 1. View blades

    We need following blades for company master

    
                       1) index.blade.php
    
                      2) create.blade.php
    
                      3) edit.blade.php
    
                      4) show.blade.php
    
                      

    So let's just create following file and put bellow code.

    1. resources/views/company/index.blade.php
    
    
                      @extends('layout')
    
                      @section('content')
    
                      <h2>Companies</h3>
                      <a href="">List All</a>   <a href="">Create New</a>  
    
                      <table class="table table-bordered">
                            <tr>
                               
                                <th>Name</th>
                                <th>Details</th>
                                <th width="280px">Action</th>
                            </tr>
                           
                            <tr>
                               
                                <td> Company !</td>
                                <td> Cochin</td>
                                <td>
                                    <a href="" class="btn btn-info">View</a>
                                    <a href="" class="btn btn-info">Edit</a>
                                    <a href="" class="btn btn-info">Delete</a>
                                </td>
                            </tr>
    
                            <tr>
                               
                               <td> Company 2</td>
                               <td> Cochin</td>
                               <td>
                                   <a href="" class="btn btn-info">View</a>
                                   <a href="" class="btn btn-info">Edit</a>
                                   <a href="" class="btn btn-info">Delete</a>
                               </td>
                           </tr>
    
    
                           
                        </table>
    
    
                        @endsection
    
                    
    2. resources/views/company/create.blade.php
    
    
                    @extends('layout')
    
                    @section('content')
    
    
                    <form action="" method="POST">
                     @csrf
                    
                      <div class="row">
                          <div class="col-xs-12 col-sm-12 col-md-12">
                              <div class="form-group">
                                  <strong>Name:</strong>
                                  <input type="text" name="name" class="form-control" placeholder="Name">
                              </div>
                          </div>
                          <div class="col-xs-12 col-sm-12 col-md-12">
                              <div class="form-group">
                                  <strong>Detail:</strong>
                                  <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
                              </div>
                          </div>
                          <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                                  <button type="submit" class="btn btn-primary">Submit</button>
                          </div>
                      </div>
                    
                  </form>
    
    
                      @endsection
    
                    
    Please not that
    1. @csrf
    2. method="POST" in the form tag
    3. resources/views/company/edit.blade.php
    
    
                    @extends('layout')
    
                    @section('content')
    
    
                    <form action="" method="POST">
                        @csrf
                        @method('PUT')
                  
                        <div class="row">
                            <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                    <strong>Name:</strong>
                                    <input type="text" name="name" value="" class="form-control" placeholder="Name">
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                    <strong>Detail:</strong>
                                    <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                              <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
                  
                    </form>
    
    
                      @endsection
    
                    
    Please not that
    1. @method('PUT') is used to implement the PUT route
    2. value attribute to show the value in the form
    4. resources/views/company/show.blade.php
    
    
                    @extends('layout')
    
                    @section('content')
    
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12">
                            <div class="form-group">
                                <strong>Name:</strong>
                                Company 1
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-12">
                            <div class="form-group">
                                <strong>Details:</strong>
                               Cochin
                            </div>
                        </div>
                    </div>
    
    
                      @endsection
    
                    

    STEP 2. Controller

    We should create new controller as CompanyController to show the above blades

    Normally a controller should have 7 methods as bellow methods:
    
    1)index()
    
    2)create()
    
    3)store()
    
    4)show()
    
    5)edit()
    
    6)update()
    
    7)destroy()
    
    

    Let us create above functions and show the blades

    app/Http/Controllers/CompanyController.php
    
      
    namespace App\Http\Controllers;
       
       use App\Models\Product;
       use Illuminate\Http\Request;
         
       class ProductController extends Controller
       {
           
           public function index()
           {          
           
               return view('company.index');
           }
            
          
           public function create()
           {
    
               return view('company.create');
           }
           
        
           public function store(Request $request)
           {
             
            
              
           }        
          
           public function show($id)
           {
            
               return view('company.show');
           } 
            
         
           public function edit(Product $product)
           {
    
               return view('company.edit');
           }
           
          
           public function update(Request $request, $id)
           {
             
    
           }
           
         
           public function destroy($id)
           {
              
    
           }
       }
    
      
    Note that the function name, parameters and return function

    STEP 3. Define the routes for above functions

    routes/web.php

    
                        use App\Http\Controllers\CompanyController;
    
    
                        Route::get('companies', [CompanyController::class, 'index']);
    
                        Route::get('companies/create', [CompanyController::class, 'create']);
                        Route::post('companies', [CompanyController::class, 'store']);
    
    
                        Route::get('companies/{id}', [CompanyController::class, 'show']);
    
    
                        Route::get('companies/{id}/edit', [CompanyController::class, 'edit']);
                        Route::put('companies/{id}', [CompanyController::class, 'update']);
    
    
                        Route::delete('companies/{id}', [CompanyController::class, 'destroty']);
                     
    
    
    
                        

    STEP 4. Call the routes in the blades

    1. to show listing page, use the route url('companies') in 'a href' tag
    2. to show create form, use the route url('companies/create') in 'a href' tag

    open resources/views/company/index.blade.php and add the url

    3. to save the form data, use the route url('companies') in the action of form tag

    open resources/views/company/create.blade.php

  • 2. Business Logic

    STEP 5. Table and Model

    1. Create table 'companies' with fields id, name and details

    2. Create model 'Company.php'

    app/Models/Company.php
    
                    namespace App\Models;
      
                    use Illuminate\Database\Eloquent\Factories\HasFactory;
                    use Illuminate\Database\Eloquent\Model;
                        
                    class Company extends Model
                    {
                        use HasFactory;
                        
                        protected $fillable = [
                            'name', 'detail'
                        ];
                    }
    
                    

    STEP 6 . Save data into db

    1. Open the controller app/Http/Controllers/CompanyController.php

    2. include the Company model in the controller CompanyController.php

    3. add the given code inside the function store()

    
    
                    public function store(Request $request)
                    {
                            $company= new Company;
                           
                            $company->name= $request->name;
                            $company->details= $request->details;
                            $company->save();
    
    
                    }
                    

    STEP 7 . Get all data to show in the listing page

    1. Open the controller app/Http/Controllers/CompanyController.php

    2. include the Company model in the controller CompanyController.php

    3. add the given code inside the function index()

    
    
                    public function index(Request $request)
                    {
                            $data['company']=Company::get();                       
                            return view('company.index',  $data);
    
                    }
                    

    STEP 8 . Get the data to show in the view page

    1. Open the controller app/Http/Controllers/CompanyController.php

    2. include the Company model in the controller CompanyController.php

    3. add the given code inside the function show()

    
    
                    public function show(Request $request, $id)
                    {
                            $data['company']=Company::find($id);                       
                            return view('company.show',  $data);
    
                    }
                    

    STEP 9 . Get the data to show in the form page for editing

    1. Open the controller app/Http/Controllers/CompanyController.php

    2. include the Company model in the controller CompanyController.php

    3. add the given code inside the function edit()

    
    
                    public function edit(Request $request, $id)
                    {
                            $data['company']=Company::find($id);                       
                            return view('company.edit',  $data);
    
                    }
                    

    STEP 10 . Delete the data

    1. Open the controller app/Http/Controllers/CompanyController.php

    2. include the Company model in the controller CompanyController.php

    3. add the given code inside the function destroy()

    
    
                    public function destroy(Request $request, $id)
                    {
                            $company=Company::find($id)->delete();                        
    
                    }
                    
  • 3. Data Presentation

    11. Show all data in the listing page

    1. open resources/views/company/index.blade.php

    2. display the data '$company' which we passed in the index()

    3. update the route of View button - url('companies/{id}')

    4. update the route of Edit button - url('companies/{id}/edit')

    5. update the route of Delete button - url('companies/{id}')

    Complete code of index.blade.php
    
    
                    <pre><xmp>
    
                      @extends('layout')
    
                      @section('content')
    
                      <h2>Companies</h3>
                      <a href="{{url(‘companies')}}">List All</a>   <a href="{{url(‘companies/create')}}">Create New</a>  
    
                      <table class="table table-bordered">
                            <tr>
                               
                                <th>Name</th>
                                <th>Details</th>
                                <th width="280px">Action</th>
                            </tr>
                           
                                     
                        @foreach($company as $row) 
                            <tr>
                                                    
                                            <td> {{$row[‘name’]}}</td>
                                            <td> {{$row[‘details’]}}</td>
                                            <td>
                                                <a href="{{url(‘companies/’.$row[‘id’])}}" class="btn btn-info">View</a>
                                                <a href="{{url(‘companies/’.$row[‘id’].’/edit’)}}" class="btn btn-info">Edit</a>
                                                        
                                                <form action="{{url(‘companies/’.$row[‘id’])}}" method="POST">
                                                            @csrf
                                                            @method('DELETE')    
                                                            <button type="submit" class="btn btn-danger">Delete</button>
                                                 </form>
    
                                            </td>
                            </tr>
                            @endoreach
    
    
                           
                        </table>
    
    
                        @endsection
    
             
    
    
                

    11. Show data in the edit form

    1. open the file resources/views/company/edit.blade.php

    2. use 'value' attribute for showing the data in the input element

    Complete code of resources/views/company/edit.blade.php
    
       
    
                    @extends('layout')
    
                    @section('content')
    
    
                    <form action="" method="POST">
                        @csrf
                        @method('PUT')
    
                        <div class="row">
                            <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                    <strong>Name:</strong>
                                    <input type="text" name="name" value="{{$company[‘name’]}}" class="form-control" placeholder="Name">
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                    <strong>Detail:</strong>
                                    <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{$company[‘name’]}}</textarea>
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                            <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
    
                    </form>
    
    
                    @endsection
    
                    

    13. save the edited data in the edit form

    3. add url('companies/{id}') to the action of the form tag in edit.blade.php

    14. Display the data in the show.blade.php

    1. open resources/views/company/show.blade.php

    2. dipaly the data